// PROGRAMA : SCRP1600
// SISTEMA : SISP - SISTEMA DE INFORMACOES DE SEGURANCA PUBLICA
// IDENTIFICACAO CRIMINAL
// BIBLIOTECA : SISP
// CLIENTE : GEJUSPC - GERENCIA DE ESTADO DA JUSTICA, SEGURANCA
// PUBLICA E CIDADANIA / INSTITUTO DE IDENT.
// OBJETIVO : RELATORIOS
// PROGRAMADOR: JOSE ANTONIO COELHO
// DATA : 02 DE SETEMBRO DE 1997
// ALTERACAO : 14/06/1999 POR SOLON
package br.com.vti.sisp_sicrim.antigo.controller.programa;

import java.io.FileNotFoundException;
import java.text.ParseException;

import net.sf.jasperreports.engine.JRException;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Naoencontrado;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Scrm1600;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Scrp1600A;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Scrp1600B;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Scrp1600C;
import br.com.vti.sisp_sicrim.antigo.util.ExceptionProgram;
import br.com.vti.sisp_sicrim.antigo.util.Scrg001;
import br.com.vti.sisp_sicrim.antigo.util.Sessao;
import br.com.vti.sisp_sicrim.antigo.util.UtilBean;

/**
 * Classe : Scrp1600 OBJETIVO : RELATORIOS *
 * 
 * @author Incode - Data: mai/2014
 * 
 */
@Resource
@Path("/antigo/scrp1600")
public class Scrp1600 {
	// Views utilizadas
	// Telas/relatorios utilizados
	Scrm1600 scrm1600;
	Scrp1600A scrp1600A;
	Scrp1600B scrp1600B;
	Scrp1600C scrp1600C;
	// Includes utilizados
	// Areas globais / session
	Scrg001 scrg001;
	// Funcoes/subrotinas
	FuncoesGerais funcoes = new FuncoesGerais();
	Result result;
	Sessao sessao;
	Boolean primeiraVez = false;
	Scrp1600 scrp1600Anterior;
	String faseproc = "0";
	// private String[] jOpc = new String[5];
	private int jDataIni = 0;
	private int jDataFim = 0;
	private int jData = UtilBean.obtemDataCorrente();
	private String mensagemInicial = "";

	private Scrm1600 preMapas() {
		sessao.getScrg001().setWVltw(10);
		sessao.getScrg001().setWProgramaw(this.getClass().getSimpleName().toUpperCase());
		sessao.getScrg001().setWFuncaow("FICHAS CRIMINAIS");

		int ind;

		scrm1600 = new Scrm1600(true, sessao);
		scrp1600A = new Scrp1600A(true, sessao);
		scrp1600B = new Scrp1600B(true, sessao);
		scrp1600C = new Scrp1600C(true, sessao);
		for (ind = 1; ind <= 4; ind++) {
			scrm1600.setJopc(ind, "");
		}

		return scrm1600;
	}

	public Scrp1600(Result result, Sessao sessao) {
		this.result = result;
		this.sessao = sessao;
	}

	@Post
	public void executar(Scrm1600 scrm1600, Scrp1600A scrp1600A, Scrp1600B scrp1600B, Scrp1600C scrp1600C) {
		try {
			main(scrm1600, scrp1600A, scrp1600B, scrp1600C);
		} catch (Exception e) {
			ExceptionProgram excp = new ExceptionProgram();
			excp.trataErroScrcerro(e, sessao, this.getClass().getSimpleName());
		}
	}

	private void main(Scrm1600 pscrm1600, Scrp1600A pscrp1600A, Scrp1600B pscrp1600B, Scrp1600C pscrp1600C) throws FileNotFoundException, ParseException, JRException {
		if (pscrm1600 == null && pscrp1600A == null && pscrp1600B == null && pscrp1600C == null) {
			primeiraVez = true;
			// scrm1600 = new Scrm1600 (primeiraVez);
			scrm1600 = preMapas();
			scrm1600.setNavegar("../scrp1600/executar");
			if (!mensagemInicial.equals(""))
				scrm1600.setMensagem(mensagemInicial);
			result.include("scrm1600", scrm1600);
			sessao.setAtributo("SCRP1600", this);
			scrm1600.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return;
		} else {
			restaurarEstados(pscrm1600, pscrp1600A, pscrp1600B, pscrp1600C);
		}

		scrm1600.receberTela(false, result);
		switch (scrm1600.getLincol()) { // *CURSOR
		case 11044: {
			// if (!verificaData(scrp1600C)) {
			// return;
			// }
			// funcoes.scrpbath();

			sessao.setAtributo("SCRP1600", this);
			Naoencontrado ne = new Naoencontrado();
			ne.setNavegar("../scrp1600/executar");
			ne.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return;

		}
		case 13047: {
			if (!scrp1600A.getEnviada()) {
				scrp1600A.setEnviada(true);
				sessao.setAtributo("SCRP1600", this);
				result.include("scrp1600A", scrp1600A);
				scrp1600A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return;
			}
			if (pscrp1600A != null) {
				scrp1600A.receberTela(false, result);
			}
			// SCRP1600
			if (!((UtilBean.funcao.equals(UtilBean.ctt_retorna) || UtilBean.funcao.equals(UtilBean.ctt_processa)))) {
				scrp1600A.enviarMensagem(null, "Tecla invalida");
				sessao.setAtributo("SCRP1600", this);
				result.include("scrp1600A", scrp1600A);
				scrp1600A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return;
			}
			if (UtilBean.funcao.equals(UtilBean.ctt_retorna)) {
				sessao.setAtributo("SCRP1600", this);
				result.include("scrm1600", scrm1600);
				scrm1600.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return;
			}
			if (scrp1600A.getJficha1() == 0) {
				scrp1600A.enviarMensagem(null, "Informe o numero da ficha");
				sessao.setAtributo("SCRP1600", this);
				result.include("scrp1600A", scrp1600A);
				scrp1600A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return;
			}
			if (scrp1600A.getJficha2() == 0) {
				scrp1600A.enviarMensagem("jFicha2", "Informe o numero da ficha");
				sessao.setAtributo("SCRP1600", this);
				result.include("scrp1600A", scrp1600A);
				scrp1600A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return;
			}
			if (scrp1600A.getJficha2() < scrp1600A.getJficha1()) {
				scrp1600A.enviarMensagem("jFicha2", "No.final e� menor que inicial");
				sessao.setAtributo("SCRP1600", this);
				result.include("scrp1600A", scrp1600A);
				scrp1600A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return;
			}
			Scrp1640 scrp1640 = new Scrp1640(result, sessao);
			scrp1640.executar(scrp1600A.getJficha1(), scrp1600A.getJficha2());
			return;
		}
		case 15050: {
			if (verificaData(pscrp1600C)) {
				Scrp1620 scrp1620 = new Scrp1620(result, sessao);
				scrp1620.jDataIni = jDataIni;
				scrp1620.executar(null);
			}
			return;
		}
		case 17053: {

			if (!scrp1600B.getEnviada()) {
				scrp1600B.setEnviada(true);
				sessao.setAtributo("SCRP1600", this);
				result.include("scrp1600B", scrp1600B);
				scrp1600B.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return;
			}
			if (pscrp1600B != null) {
				scrp1600B.receberTela(false, result);
			}
			if (!((UtilBean.funcao.equals(UtilBean.ctt_retorna) || UtilBean.funcao.equals(UtilBean.ctt_processa)))) {
				scrp1600B.enviarMensagem(null, "Tecla invalida");
				sessao.setAtributo("SCRP1600", this);
				result.include("scrp1600B", scrp1600B);
				scrp1600B.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return;
			}
			if (UtilBean.funcao.equals(UtilBean.ctt_retorna)) {
				Scrp1600 scrp1600 = new Scrp1600(result, sessao);
				scrp1600.executar(null, null, null, null);
				return;
			}

			if (!validarNumeros(scrp1600B.getJdiaini(), scrp1600B.getJmesini(), scrp1600B.getJanoini())) {
				scrp1600B.enviarMensagem("jDiaIni", "Data Inicial invalida");
				sessao.setAtributo("SCRP1600", this);
				result.include("scrp1600B", scrp1600B);
				scrp1600B.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return;
			}
			if (validarNumeros(scrp1600B.getJanoini()) && Integer.parseInt(scrp1600B.getJanoini()) < 1997) {
				scrp1600B.enviarMensagem("jAnoIni", "Ano invalido");
				sessao.setAtributo("SCRP1600", this);
				result.include("scrp1600B", scrp1600B);
				scrp1600B.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return;
			}

			jDataIni = Integer.valueOf(UtilBean.inverteDataToYYYYMMDD(scrp1600B.getJdiaini() + scrp1600B.getJmesini() + scrp1600B.getJanoini()));
			if (jDataIni > jData) {
				scrp1600B.enviarMensagem("jDiaIni", "Data invalida");
				sessao.setAtributo("SCRP1600", this);
				result.include("scrp1600B", scrp1600B);
				scrp1600B.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return;
			}
			if (!validarNumeros(scrp1600B.getJdiafim(), scrp1600B.getJmesfim(), scrp1600B.getJanofim())) {
				scrp1600B.enviarMensagem("jDiaFim", "Data Final invalida");
				sessao.setAtributo("SCRP1600", this);
				result.include("scrp1600B", scrp1600B);
				scrp1600B.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return;
			}
			if (validarNumeros(scrp1600B.getJanofim()) && Integer.parseInt(scrp1600B.getJanofim()) < 1997) {
				scrp1600B.enviarMensagem("jAnoFim", "Ano invalido");
				sessao.setAtributo("SCRP1600", this);
				result.include("scrp1600B", scrp1600B);
				scrp1600B.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return;
			}
			jDataFim = Integer.valueOf(UtilBean.inverteDataToYYYYMMDD(scrp1600B.getJdiafim() + scrp1600B.getJmesfim() + scrp1600B.getJanofim()));
			if (jDataFim < jDataIni || jDataFim > jData) {
				scrp1600B.enviarMensagem("jDiaFim", "Data final invalida");
				sessao.setAtributo("SCRP1600", this);
				result.include("scrp1600B", scrp1600B);
				scrp1600B.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return;
			}
			jDataIni = Integer.parseInt(scrp1600B.getWdataIni());
			jDataFim = Integer.parseInt(scrp1600B.getWdataFim());
			Scrp1630 scrp1630 = new Scrp1630(result, sessao);
			scrp1630.jDataIni = jDataIni;
			scrp1630.jDataFim = jDataFim;
			scrp1630.executar(null);
			break;
		}
		default: {
			break;
		}
		}
		sessao.setAtributo("SCRP1600", this);
		result.include("scrm1600", scrm1600);
		if (!result.used())
			scrm1600.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
	}

	private boolean verificaData(Scrp1600C pscrp1600C) {

		if (!scrp1600C.getEnviada()) {
			scrp1600C.setEnviada(true);
			sessao.setAtributo("SCRP1600", this);
			result.include("scrp1600C", scrp1600C);
			scrp1600C.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return false;
		}
		if (pscrp1600C != null) {
			scrp1600C.receberTela(false, result);
		}
		if (!((UtilBean.funcao.equals(UtilBean.ctt_retorna) || UtilBean.funcao.equals(UtilBean.ctt_processa)))) {
			scrp1600C.enviarMensagem(null, "Tecla invalida");
			sessao.setAtributo("SCRP1600", this);
			result.include("scrp1600C", scrp1600C);
			scrp1600C.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return false;
		}
		if (UtilBean.funcao.equals(UtilBean.ctt_retorna)) {
			sessao.setAtributo("SCRP1600", this);
			result.include("scrm1600", scrm1600);
			scrm1600.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return false;
		}
		if (!validarNumeros(scrp1600C.getJdiaini(), scrp1600C.getJmesini(), scrp1600C.getJanoini())) {
			scrp1600C.enviarMensagem("wdiaini", "Data invalida");
			sessao.setAtributo("SCRP1600", this);
			result.include("scrp1600C", scrp1600C);
			scrp1600C.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return false;
		}
		if (validarNumeros(scrp1600C.getJanoini()) && Integer.parseInt(scrp1600C.getJanoini()) < 1997) {
			scrp1600C.enviarMensagem("janoini", "Ano invalido");
			sessao.setAtributo("SCRP1600", this);
			result.include("scrp1600C", scrp1600C);
			scrp1600C.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return false;
		}
		jDataIni = Integer.valueOf(UtilBean.inverteDataToYYYYMMDD(scrp1600C.getJdiaini() + scrp1600C.getJmesini() + scrp1600C.getJanoini()));
		if (jDataIni > jData) {
			scrp1600C.enviarMensagem("jdiaini", "Data invalida");
			sessao.setAtributo("SCRP1600", this);
			result.include("scrp1600C", scrp1600C);
			scrp1600C.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return false;
		}
		return true;
	}

	private void restaurarEstados(Scrm1600 pscrm1600, Scrp1600A pscrp1600A, Scrp1600B pscrp1600B, Scrp1600C pscrp1600C) {
		this.scrp1600Anterior = (Scrp1600) sessao.getAtributo("SCRP1600");
		if (pscrm1600 != null) {
			scrm1600 = pscrm1600;
			scrm1600.setJempresaj(sessao.getScrg001().getWEmpresaw());
			scrm1600.setJversaoj(sessao.getScrg001().getWVersaow());
			scrm1600.setJprogramaj(sessao.getScrg001().getWProgramaw());
			scrm1600.setJfuncaoj(sessao.getScrg001().getWFuncaow());
		} else {
			scrm1600 = this.scrp1600Anterior.scrm1600;
		}
		scrm1600.setSessao(sessao);
		if (pscrp1600A != null) {
			scrp1600A = pscrp1600A;
			scrp1600A.setJempresaj(sessao.getScrg001().getWEmpresaw());
			scrp1600A.setJversaoj(sessao.getScrg001().getWVersaow());
			scrp1600A.setJprogramaj(sessao.getScrg001().getWProgramaw());
			scrp1600A.setJfuncaoj(sessao.getScrg001().getWFuncaow());
		} else {
			scrp1600A = this.scrp1600Anterior.scrp1600A;
		}
		scrp1600A.setSessao(sessao);
		if (pscrp1600B != null) {
			scrp1600B = pscrp1600B;
			scrp1600B.setJempresaj(sessao.getScrg001().getWEmpresaw());
			scrp1600B.setJversaoj(sessao.getScrg001().getWVersaow());
			scrp1600B.setJprogramaj(sessao.getScrg001().getWProgramaw());
			scrp1600B.setJfuncaoj(sessao.getScrg001().getWFuncaow());
		} else {
			scrp1600B = this.scrp1600Anterior.scrp1600B;
		}
		scrp1600B.setSessao(sessao);
		if (pscrp1600C != null) {
			scrp1600C = pscrp1600C;
			scrp1600C.setJempresaj(sessao.getScrg001().getWEmpresaw());
			scrp1600C.setJversaoj(sessao.getScrg001().getWVersaow());
			scrp1600C.setJprogramaj(sessao.getScrg001().getWProgramaw());
			scrp1600C.setJfuncaoj(sessao.getScrg001().getWFuncaow());
		} else {
			scrp1600C = this.scrp1600Anterior.scrp1600C;
		}
		scrp1600C.setSessao(sessao);
		this.faseproc = this.scrp1600Anterior.faseproc;
		scrp1600C.setEnviada(this.scrp1600Anterior.scrp1600C.getEnviada());
		scrp1600A.setEnviada(this.scrp1600Anterior.scrp1600A.getEnviada());
		scrp1600B.setEnviada(this.scrp1600Anterior.scrp1600B.getEnviada());
		if (this.scrp1600Anterior.scrm1600.getLincol() != 0)
			scrm1600.setLincol(this.scrp1600Anterior.scrm1600.getLincol());
	}

	protected void setMensagem(String mensagem) {
		mensagemInicial = mensagem;
	}

	private boolean validarNumeros(String... numeros) {
		for (String numero : numeros) {
			if (numero == null || numero.isEmpty()) {
				return false;
			} else {
				for (int i = 0; i < numero.length(); i++) {
					char ch = numero.charAt(i);
					if (ch < 48 || ch > 57) {
						return false;
					}
				}
			}
		}
		return true;
	}
}
